﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using UnitFundProfitability.BusinessLogic;

namespace UnitFundProfitability
{
    /// <summary>
    /// Interaction logic for UnitFunds.xaml
    /// </summary>
    public partial class UnitFunds : Window
    {
        #region Constructors
        public UnitFunds()
        {
            InitializeComponent();
            ExistingCompanies.DataContext = Manager.Instance.GetManagementCompanies();
            ExistingCompanies.SelectionChanged += new SelectionChangedEventHandler(ExistingCompanies_SelectionChanged);
            RefreshExistingUnitFunds();
            AddUnitFund.Click += new RoutedEventHandler(AddUnitFund_Click);
        }
        #endregion

        #region Methods
        private void RefreshExistingUnitFunds()
        {
            ManagementCompany selectedManagemetnCompany = ExistingCompanies.SelectedValue as ManagementCompany;
            if (selectedManagemetnCompany != null)
            {
                List<UnitFund> dataContext = Manager.Instance.GetUnitFunds(selectedManagemetnCompany);
                ExistingUnitFunds.DataContext = dataContext;
            }
        }
        private void ExistingCompanies_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            RefreshExistingUnitFunds();
        }
        private void AddUnitFund_Click(object sender, RoutedEventArgs e)
        {
            ManagementCompany selectedManagementCompany = ExistingCompanies.SelectedValue as ManagementCompany;
            if (selectedManagementCompany == null)
            {
                MessageBox.Show("Please, select management company.", "Select company");
            }
            else
            {
                UnitFund unitFund = new UnitFund();
                unitFund.CompanyId = selectedManagementCompany.CompanyId;
                unitFund.FundId = Guid.NewGuid();
                unitFund.FundName = UnitFundName.Text;
                Manager.Instance.AddUnitFund(unitFund);
                RefreshExistingUnitFunds();
            }
        }
        #endregion
    }
}